﻿$(function () {
    $("#file_picker_select").live("click", function (e) {
        e.preventDefault();
        $("#bgCover").show();
        $("#filePickerDlg").show();
        file_picker_loadfile($("#file_picker_root").val(), $("#file_picker_path").val(), "", $("#file_picker_ext").val());
        return false;
    });
    $("#file_picker_ok").live("click", function (e) {
        e.preventDefault();
        file_picker_close();
        var name = $("#tmp_sel_file_name").html();
        var path = $("#tmp_sel_file_path").html();
        if (name != "") {
            $("#file_picker_path").val(path);
            var root = $("#file_picker_root").val();
            if (root.charAt(root.length - 1) != "/")
                root += "/";
            $("#file_picker_display_url").html((root + path).replace("//", "/"));
        }
        return false;
    });
    $("#file_picker_cancel").live("click", function (e) {
        e.preventDefault();
        file_picker_close();
        return false;
    });
    $(".file_picker_file_list_item").live("mouseover", function () {
        $(this).css("background", "#FFF");
    });
    $(".file_picker_file_list_item").live("mouseout", function () {
        $(this).css("background", "transparent");
    });
    $("#file_picker_remove_value").live("click", function () {
        $("#file_picker_display_url").html("");
        $("#file_picker_path").val("");
    });
});
function file_picker_close() {
    $("#bgCover").hide();
    $("#filePickerDlg").hide();
}
function file_picker_loadfolder(item) {
    file_picker_loadfile($("#file_picker_root").val(), "", $(item).find("span.path").html(), $("#file_picker_ext").val());
}
function file_picker_file_selected(item) {
    var name = $(item).find("span.name").html();
    var path = $(item).find("span.path").html();
    if (name != "") {
        $("#tmp_sel_file_name").html(name);
        $("#tmp_sel_file_path").html(path);
    }
}
window.refresh_current_folder = function () {
    file_picker_loadfile($("#file_picker_root").val(), "", $("#file_picker_current_folder").html(), $("#file_picker_ext").val());
};
function file_picker_loadfile(root, file, folder, ext) {
    $.ajax({
        url: "/FilePickerExplorer/GetFiles?time=" + Math.random(),
        data: { root: root, file: file, folder: folder, ext: ext },
        dataType: "json",
        success: function (json) {
            if (json.success) {
                var table = '<table style="border-spacing:0px;width:100%;">' +
                        '<tr style="line-height:28px;text-align:center;font-weight:bold;font-size:11px;background:#ddd;">' +
                        '   <th style="width:350px; padding:3px;">Name</th>' +
                        '   <th style="width:200px; padding:3px;">Create Date</th>' +
                        '   <th style="width:100px; padding:3px;">Size</th>' +
                        '</tr>';
                $("#file_picker_folder").html((root + json.folder).replace("//", "/"));
                $("#file_picker_current_folder").html(json.folder);
                var data = json.data;
                for (var i = 0; i < data.length; i++) {
                    if (data[i].IsFolder) {
                        table = table + '<tr>' +
                                '<td>' +
                                '   <a href="javascript:void(0);" onclick="file_picker_loadfolder(this);return false;">' +
                                '       <div style="width:18px;height:20px;background: transparent url(/areas/umbraco/content/images/DocTypeIcons/TreeSprites.png) no-repeat  -8px -391px ! important;float:left;"></div>' +
                                '       <span class="name" style="float:left;">' + data[i].Name + '</span>' +
                                '       <span class="path" style="display:none;">' + data[i].Path + '</span>' +
                                '   </a>' +
                                '</td>' +
                                '<td>' + data[i].CreateDate + '</td>' +
                                '<td></td>' +
                            '</tr>';
                    }
                    else {
                        table = table + '<tr class="file_picker_file_list_item" onclick="file_picker_file_selected(this);" style="cursor:pointer;">' +
                                '<td>' +
                                '   <div>' +
                                '       <div style="width:18px;height:20px;background: transparent url(/areas/umbraco/content/images/DocTypeIcons/TreeSprites.png) no-repeat  -8px -231px ! important;float:left;"></div>' +
                                '       <span class="name" style="float:left;">' + data[i].Name + '</span>' +
                                '       <span class="path" style="display:none;">' + data[i].Path + '</span>' +
                                '   </a>' +
                                '</td>' +
                                '<td>' + data[i].CreateDate + '</td>' +
                                '<td>' + data[i].Size + '</td>' +
                            '</tr>';
                    }
                }
                table += '</table>';
                $("#file_picker_list").html(table);
            }
        }
    });
}
function file_picker_upload_file() {
    var left = (window.screen.width - 800) / 2;
    var top = (window.screen.height - 500) / 2;
    var childWindow = window.open("/FilePickerExplorer/FileUploadWindow?folder=" + $("#file_picker_folder").html(), "Upload", "width=800,height=500,left=" + left + ",top=" + top);
}
function file_picker_create_folder() {
    var folderName = prompt("The folder name:", "");
    if (folderName == "") {
        return false;
    }
    $.ajax({
        url: '/FilePickerExplorer/CreateFolder',
        dataType: 'json',
        data: { parent: $("#file_picker_folder").html(), name: folderName },
        success: function (data) {
            if (data.success) {
                window.refresh_current_folder();
            }
            else {
                alert(data.msg);
            }
        },
        error: function () {
            alert("An error has occurred while creating the folder.");
        }
    });
}